/**
 * inforcreation.com co.Ltd
 * MichaelHuang
 * 2012-2-8
 */
package com.zchat.android.views;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.zchat.api.ZChat;

/**
 * @author MichaelHuang
 *
 */
public abstract class AbstractViewLoader implements IViewLoader {
	protected Context context;
	protected ViewGroup parent;
	protected String key;
	protected ZChat zchat;
	protected View v;
	
	private boolean isExist(){
	 
		Object obj = zchat.getCache().get(key);
		
		v = (View)obj;
		
		if(v != null && v.getParent() == null) {
			parent.addView((View)obj);
			return true;
		} else if(v != null && v.getParent() != null) {
			((ViewGroup)v.getParent()).removeAllViews();
			parent.addView((View)obj);
//			parent.bringChildToFront(v);
			return true;
		}
		return false;
	}
	
	@Override
	public void startLoader(Context context, ViewGroup parent, 
			String key,ZChat zchat) {
		this.context = context;
		this.parent = parent;
		this.key = key;
		this.zchat = zchat;
		if(isExist()) {
			loadDatas(v);
		}else{
			//load view
			loadComponents();
			loadDatas(v);
		}
	}

	/** 初始化组件 */
	public abstract void loadComponents();
	
	/** 为组件赋值 */
	public abstract void loadDatas(View v);
}
